package cn.dnui_zll731.dnuifood_731zlll.Fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.List;

import cn.dnui_zll731.dnuifood_731zlll.Adapter.ShopAdapter;
import cn.dnui_zll731.dnuifood_731zlll.R;
import cn.dnui_zll731.dnuifood_731zlll.bean.ShopBean;
import cn.dnui_zll731.dnuifood_731zlll.controller.soucuoActivity;
import cn.dnui_zll731.dnuifood_731zlll.listener.RetrofitListener;
import cn.dnui_zll731.dnuifood_731zlll.model.ShopModel;

/*
    创建和使用Fragment的步骤
    1.穿件子类继承Fragment
    2.重写onCreateView方法该方法主要定义fragment的布局 以view对象的形式返回Fragment的视图
    3.将fragment引入到activity中
 */
public class Fragment1 extends BaseFragment implements RetrofitListener<List<ShopBean>> {
    private LinearLayout linearLayout;
    private ShopAdapter shopAdapter;
    @Override
    protected int initlayout() {
        return R.layout.fragment1;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater,container,savedInstanceState);
    }
    @Override
    void initViews() {
        linearLayout = view.findViewById(R.id.f1_sousuo);
        recyclerView = view.findViewById(R.id.rv01);
    }

    @Override
    void initData() {
        ShopModel shopModel = new ShopModel();
        shopModel.getAllShops(this);
        Log.d("调试", "initData: 运行");
    }

    @Override
    void initEvents() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(
                getActivity(),
                LinearLayoutManager.VERTICAL,
                false
        );//布局效果---垂直布局
        recyclerView.setLayoutManager(linearLayoutManager);//设置效果
        recyclerView.setItemAnimator(new DefaultItemAnimator());//设置动画效果

        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(),soucuoActivity.class);
                startActivity(intent);
            }
        });
    }
    /*
         1.表示Fragment第一次创建绘制用户界面时系统回调的方法
         2.返回值view表示当前加载fragment视图 如果fragment不提供视图可以返回null
         3-1.LayoutInflater inflater :表示布局填充器或者加载器,将xml文件转换成view对象
         3-2.ViewGroup container,:表示当前fragment插入activity的布局对象
         3-3.Bundle savedInstanceState :表示储存上一个Fragment的信息
         */
    @Override
    public void onSuccess(List<ShopBean> shopBeans, int flag) {
        //设置适配器
        Log.d("调试", "onSuccess: 1");

        shopAdapter = new ShopAdapter(getActivity(),shopBeans,R.layout.shop_item);
        recyclerView.setAdapter(shopAdapter);

    }

    @Override
    public void onFalure() {
        Toast.makeText(getActivity(),"网络错误",Toast.LENGTH_SHORT).show();
    }
}
